#include "userpage.h"
#include "ui_userpage.h"

UserPage::UserPage(StuData stuData, std::map<QString, SeatInfo> seatData, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::UserPage)
{
    ui->setupUi(this);
    UpdateUserState(stuData);
    UpdateSeatTable(seatData);
    ui->label->setText("当前用户："+stuData.name + "  " + stuData.stuid);
}

UserPage::~UserPage()
{
    delete ui;
}

/**
 * @brief UserPage::on_pushButtonEditPasswd_clicked 修改当前用户的密码
 */
void UserPage::on_pushButtonEditPasswd_clicked()
{
    bool res = false;
    stuData_.passwd = QInputDialog::getText(this, "", "请输入新密码", QLineEdit::Password, "", &res);
    if (res && !stuData_.passwd.isEmpty())
    {
        emit PostEditUserPassword(stuData_);
    }
}

QString randstr(const int len)
{
    QString str;
    srand(time(NULL));
    int i;
    for (i = 0; i < len; ++i)
    {
        str += '0' + rand() % 10;
    }
    return str;
}

/**
 * @brief UserPage::on_pushButtonRes_clicked 按下预约/签退按钮后，进行相应的预约/签退操作
 */
void UserPage::on_pushButtonRes_clicked()
{
    QString seatid = ui->tableViewSeat->model()->index(ui->tableViewSeat->currentIndex().row(), 0).data().toString();
    if (seatid.isEmpty() && stuData_.resState == RESSTATE_NULL)
    {
        QMessageBox::warning(NULL, "", "请先选择需要预约的座位", QMessageBox::Yes);
        return;
    }
    else if (stuData_.resState != RESSTATE_NULL)
    {
        seatid = stuData_.resSeatAddr;
    }
    std::map<QString, SeatInfo>::iterator iter = seatData_.find(seatid);
    if (iter == seatData_.end())
        return;
    SeatInfo seatInfo = iter->second;
    if (!seatInfo.isOnline)
    {
        QMessageBox::warning(NULL, "", "该座位已离线，请重新选择。", QMessageBox::Yes);
        return;
    }
    if (seatInfo.seatState != SEATSTATE_FREE && seatInfo.stuid.compare(stuData_.stuid) != 0)
    {
        QMessageBox::warning(NULL, "", "该座位正在被使用，请重新选择。", QMessageBox::Yes);
        return;
    }
    if (stuData_.limit == 1)
    {
        QMessageBox::warning(NULL, "", "由于连续三次预约座位且没有进行签到，该账号已被冻结。\n解冻时间：" + QDateTime::fromTime_t(stuData_.limitTime.toUInt()).toString(), QMessageBox::Yes);
        return;
    }
    if (stuData_.resState == RESSTATE_NULL)
    {
        QString resTime = QString::number(ui->dateTimeEditStart->dateTime().toTime_t());
        QString resTimeStr = QDateTime::fromTime_t(resTime.toUInt()).toString();
        qDebug() << "resTime:" << resTime << "resTimeStr" << resTimeStr;
        seatInfo.startTime = resTime;
        seatInfo.stuid = stuData_.stuid;
        seatInfo.seatState = SEATSTATE_BUSY;
        emit PostEditRes(seatInfo);
        QString code = randstr(6);
        QMessageBox::information(NULL, "", "预约成功，验证码为：" + code, QMessageBox::Yes);
        emit PostCode2Seat(seatInfo.seatAddr, code);
    }
    else
    {
        seatInfo.startTime.clear();
        seatInfo.stuid = stuData_.stuid;
        seatInfo.seatState = SEATSTATE_FREE;
        emit PostEditRes(seatInfo);
    }
}

/**
 * @brief UserPage::UpdateUserState 更新用户状态
 * @param stuData
 */
void UserPage::UpdateUserState(StuData stuData)
{
    stuData_ = stuData;
    if (stuData_.resState == RESSTATE_NULL)
    {
        ui->labelResState->setText("用户尚未预约座位");
        ui->dateTimeEditStart->show();
        ui->dateTimeEditStart->setDateTime(QDateTime::currentDateTime());
        ui->labelTime->setText("");
        ui->pushButtonRes->setText("预约");
    }
    else if (stuData_.resState == RESSTATE_CHECKED_IN)
    {
        ui->labelResState->setText("用户已签到");
        QString resTimeStr = QDateTime::fromTime_t(stuData_.resTime.toUInt()).toString();
        ui->labelTime->setText(resTimeStr);
        ui->pushButtonRes->setText("签退");
        ui->label_4->hide();
        ui->dateTimeEditStart->hide();
    }
    else if (stuData_.resState == RESSTATE_NOT_CHECKED_IN)
    {
        ui->labelResState->setText("用户已预约未签到");
        QString resTimeStr = QDateTime::fromTime_t(stuData_.resTime.toUInt()).toString();
        ui->labelTime->setText(resTimeStr);
        ui->pushButtonRes->setText("取消预约");
        ui->label_4->hide();
        ui->dateTimeEditStart->hide();
    }
}

/**
 * @brief UserPage::UpdateSeatTable 更新座位信息表
 * @param seatData
 */
void UserPage::UpdateSeatTable(std::map<QString, SeatInfo> seatData)
{
    seatData_ = seatData;
    QStandardItemModel* model = new QStandardItemModel();
    QStringList labels;
    labels << "座位ip" << "座位状态";
    model->setHorizontalHeaderLabels(labels);
    ui->tableViewSeat->setModel(model);
    ui->tableViewSeat->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableViewSeat->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableViewSeat->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableViewSeat->setSelectionMode(QAbstractItemView::SingleSelection);

    //定义item
    QStandardItem* item;
    int i = 0;
    for(std::map<QString, SeatInfo>::iterator iter = seatData.begin(); iter != seatData.end(); iter++, i++){
        QString state;
        if (iter->second.isOnline == false)
            state = "设备离线";
        else state = (iter->second.seatState == SEATSTATE_FREE) ? "可被预约" : "设备正忙";
        item = new QStandardItem(state);
        model->setItem(i,1,item);
        item = new QStandardItem(iter->second.seatAddr);
        model->setItem(i,0,item);
    }
    ui->tableViewSeat->setModel(model);
}
